<template>
  <div class="seller" ref='seller'>
    <div class="seller-content">
        <!--两层div用作better-scroll页面滚动，当seller-content高度超过seller的高度时就可以滚动-->
      <div class="overview">
        <!--标题-->
        <h1 class="title">{{seller.name}}</h1>
        <!--星级，得分和月售-->
        <div class="desc border-bottom">
          <star :size='36' :score='seller.score'></star>
          <span class="text">({{seller.ratingCount}})</span>
          <span class="text">月售{{seller.sellCount}}单</span>
        </div>
        <!-- 起送，配送，配送时间列表 -->
        <ul class="remark">
          <li class="block border-right">
            <h2>起送价</h2>
            <div class="content">
              <span class="stress">{{seller.minPrice}}</span>元
            </div>
          </li>
          <li class="block border-right">
            <h2>商家配送</h2>
            <div class="content">
              <span class="stress">{{seller.deliveryPrice}}</span>元
            </div>
          </li> 
          <li class="block">
            <h2>平均配送时间</h2>
            <div class="content">
              <span class="stress">{{seller.deliveryTime}}</span>分钟
            </div>
          </li>
        </ul>
        <!-- 添加爱心收藏 -->
        <div class="favorite" @click='clicklike'>
          <span class="iconfont" :class="{'active':like}">&#xe602;</span>
          <span class="text">{{liketext}}</span>
        </div>
      </div>
      <!-- 分割线组件复用 -->
      <split></split>
      <!-- 公告与活动 -->
      <div class="bulletin">
        <h1 class="title">公告与活动</h1>
        <div class="content-wrapper border-bottom">
          <p class="content">{{seller.bulletin}}</p>
        </div>
        <!-- 活动信息 -->
        <ul class="supports">
          <li class="support-item border-clean border-bottom" v-for='(item,index) of seller.supports' :key='index'>
            <span class="icon" :class="classMap[item.type]"></span>
            <span class="text">{{item.description}}</span>
          </li>
        </ul>
      </div>
      <!-- 分割线组件复用 -->
      <split></split>
      <!-- 图片区块 -->
      <div class="pics">
        <h1>商家实景</h1>
        <div class="pic-wrapper" ref='picwrapper'>
          <ul class="pic-list" ref='piclist'>
            <li class="pic-item" v-for='(pic,index) in seller.pics' :key='index'>
              <img :src="pic">
            </li>
          </ul>
        </div>
      </div>
      <split></split>
      <!-- 商家信息 -->
      <div class="seller-info">
        <h1 class="title  border-bottom">商家信息</h1>
        <ul>
          <li class="info-list border-bottom" v-for='(item,index) in this.seller.infos' :key='index' >{{item}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import star from '../star/star'
import split from '../split/split'
import BSscroll from 'better-scroll'
import {saveToLocal,loadFromLocal} from '../../commen/js/store'

export default {
  name:'sellers',
  props:{
    seller:{//通过overview传递过来的数据
      type:Object
    }
  },
  data(){
    return {
      like:(()=>{
       return loadFromLocal(this.seller.id,'like',false)
      })()
    }
  },
  components:{
    star,
    split
  },
  computed:{
    liketext(){
      return this.like?'收藏':'未收藏'
    }
  },
  watch:{
    //异步更新，当seller数据请求成功之后，侦听器观测到seller的变化，便执行初始化scroll
    seller(){
      this.initScroll();
      this.initPics();
    }
  },
  methods:{
    clicklike(){
      this.like=!this.like;
      saveToLocal(this.seller.id,'like',this.like)
    },
    initScroll(){
      if(!this.scroll){
        this.scroll=new BSscroll(this.$refs.seller,{
            click:true
        })
      }else{
        this.scroll.refresh();//重新计算滚动的高度
      }
    },
    initPics(){
      if(this.seller.pics){
      let picwidth=120 
      let margin=6
      let width=(picwidth+ margin)*this.seller.pics.length-margin
      //better-scroll滚动需要内部的盒子长度大于外部盒子长度，才可以实现滚动
      this.$refs.piclist.style.width=width+'px'
      this.$nextTick(()=>{
         if(!this.picscroll){
          this.picscroll=new BSscroll(this.$refs.picwrapper,{
              scrollX:true,//横向滚动
              //外层区块竖向滚动，内层区块横向滚动，该属性控制横向滚动的时候忽略竖向滚动
              eventPassthrough:'vertical'
          })
         }else{
           this.picscroll.refresh();
         }
        })
      }
    }
    
  },
  created(){
    this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
  },
  mounted(){
    this.initScroll();
    this.initPics();
  }
}
</script>

<style lang="stylus" scoped>
@import "../../commen/stylus/mixin.styl";
.seller
  position:absolute
  top:174px 
  bottom:0
  left:0 
  width:100%
  overflow:hidden //固定了视口的高度
  .overview
    padding:18px 
    position:relative
    .title 
      margin-bottom:8px
      font-size:14px 
      color:rgb(7,17,27)
      line-height:14px 
    .desc 
      padding-bottom:18px
      line-height:18px 
      font-size:0
      .star 
        display:inline-block
        vertical-align:top
        margin-right:8px 
      .text 
        display:inline-block
        vertical-align:top
        margin-right:12px
        font-size:10px 
        color:rgb(77,85,93)
    .remark 
      display:flex 
      padding-top:18px
      .block 
        flex:1
        text-align:center
        h2 
          margin-bottom:4px
          line-height:10px
          font-size:10px 
          color:rgb(147,153,159)
        .content,.stress
          line-height:24px
          font-size:10px 
          color:rgb(7,17,27)
          font-weight:200
        .stress 
          font-size:24px 
    .favorite 
      position:absolute 
      top:18px 
      right:11px
      width:50px
      text-align:center
      .iconfont
        display:block
        font-size:24px
        margin-bottom:4px
        color:#d4d6d9
      .active 
        color:red
      .text 
        line-height:10px
        text-align:center
        font-size:10px 
        color:rgb(77,85,93)
  .bulletin
    padding:18px 18px 0 18px
    .title 
      margin-bottom:8px
      font-size:14px 
      color:rgb(7,17,27)
      line-height:14px 
    .content-wrapper 
      padding:0 12px 16px 12px
      .content 
        font-size:12px 
        font-weight:200 
        color:rgb(240,20,20)
        line-height:24px
    .supports 
      .support-item
        padding:16px 12px
        font-size:0
        &:last-child:before
          border:none
        .icon 
          display:inline-block
          vertical-align:middle
          width:16px 
          height:16px 
          margin-right:6px
          background-size: 16px 16px
          background-repeat: no-repeat
          &.decrease
            bg-image('decrease_4')
          &.discount
            bg-image('discount_4')
          &.guarantee
            bg-image('guarantee_4')
          &.invoice
            bg-image('invoice_4')
          &.special
            bg-image('special_4')
        .text 
          display:inline-block
          vertical-align:middle
          font-size:12px
          font-weight:200 
          color:rgb(7,17,27)
          line-height:18px
  .pics
    padding:18px
    h1 
      margin-bottom:8px
      font-size:14px 
      color:rgb(7,17,27)
      line-height:14px 
    .pic-wrapper
      width:100%
      overflow:hidden 
      white-space:nowrap//不折行
      .pic-list
        font-size:0
        .pic-item 
          display:inline-block
          margin-right:6px
          &:last-child
            margin:0 //最后一个边距为0
          img 
            width:120px 
            height:90px
  .seller-info
    padding:18px 18px 0 18px
    color:rgb(7,17,27)
    .title 
      padding-bottom:12px
      line-height:14px
      font-size:14px 
    .info-list 
      padding:16px 12px
      line-height:16px
      font-size:12px 
      font-weight:200 
      &:last-child:before 
        border:none
</style>